﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GeoSketch2.Core;
using System.Windows.Forms;
using System.Drawing;

namespace GeoSketch2.Archaeo
{
    /// <summary>
    /// The ruler tool, gets the distance between 2 points
    /// </summary>
    public class TRuler:BaseArchaeoTool
    {
        #region Fields

        //True if the second point of the line has not been clicked
        bool _ClicRemaining;

        Point2D FirstPoint;
        Point2D SecondPoint;
        #endregion

        #region Constructor
        /// <summary> 
        /// The ruler tool
        /// </summary>
        /// <param name="Viewport">The viewport to draw to</param>
        public TRuler(Viewport Viewport)
            : base(Viewport,"Ruler")
        {
            _ClicRemaining = false;
        } 
        #endregion

        #region Inputs
        /// <summary>
        /// The method called when the mouse button is down
        /// </summary>
        public override void MouseDown(MouseEventArgs MouseArgs)
        {
            ExternalPointSent(Viewport.Object2ImageTransformation.InverseTransform(MouseArgs.Location));
        }

        /// <summary>
        /// The method called when a text is sent to the tool
        /// </summary>
        public override void TextEntry(string Text)
        {
            ExternalPointSent(GetCoordinatesFromString(Text));
        }

        /// <summary>
        /// The method called when a point in object coordinates is sent to the tool
        /// </summary>
        public override void ExternalPointSent(Point2D64D Point)
        {
            if (!_ClicRemaining)
            {
                FirstPoint = new Point2D(Point, null, null);
                SendMessage("Click second point");
                SecondPoint = null;
            }
            else
            {
                SecondPoint = new Point2D(Point, null, null);
                SendMessage(FirstPoint.Position.Distance2D(Point).ToString("F3", Core.Vars.Setup.GeoSketch.NumberFormat) + " - Click first point");
            }
            _ClicRemaining = !_ClicRemaining;
            RefreshViewport();
        }
        #endregion

        #region Methods
        /// <summary>
        /// Draw the graphics requested by the object
        /// </summary>
        public override void Draw()
        {
            if (FirstPoint != null)
            {
                FirstPoint.DrawCustomPen(Viewport, new System.Drawing.Pen(Color.Red));
            }
            if (SecondPoint != null)
            {
                SecondPoint.DrawCustomPen(Viewport, new System.Drawing.Pen(Color.Red));
            }
        }

        /// <summary>
        /// The method to be used to send the first message to the user, just after beeing selected
        /// </summary>
        public override void SendFirstMessage()
        {
            SendMessage("Click first point");
        } 
        #endregion
    }
}